package com.blueseaapps;

import com.blueseaapps.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CategorySelectActivity extends Activity{
	private Integer[] categoryIconIds = {
			R.drawable.current_affairs,
			R.drawable.logos,
			R.drawable.sports,
			R.drawable.celebrities,
			R.drawable.literature,
			R.drawable.travel,
			R.drawable.entertainment,
			R.drawable.technology,
			R.drawable.history,
			R.drawable.flags,
			R.drawable.icon
	};

	private CharSequence[] categoryNames = {
			"Current Affairs",
			"Company Logos",
			"Sports",
			"Celebrities",
			"Literature",
			"Travel",
			"Entertainment",
			"Technology",
			"History",
			"Flags",
			"All"
	};

	GridView grid_main;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_select);
		grid_main = (GridView)findViewById(R.id.cat_select_view);
		grid_main.setAdapter(new ImageAdapter(this));

		grid_main.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> view, View arg1, int pos,
					long posId) {
				Log.d("Category", categoryNames[pos].toString());
				Intent beginIntent = new Intent(view.getContext(),MainActivity.class);
				beginIntent.putExtra(MainActivity.SELECTED_CATEGORY_KEY, categoryNames[pos].toString());
				startActivity(beginIntent);
				finish();
			}
		});
	}
	public class ImageAdapter extends BaseAdapter{
		public ImageAdapter(Context c){
		}
		@Override
		public int getCount() {
			return categoryIconIds.length;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v;
			if(convertView==null){
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.icon, null);
				TextView tv = (TextView)v.findViewById(R.id.icon_text);
				tv.setText(categoryNames[position]);
				ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
				iv.setImageResource(categoryIconIds[position]);
				iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
				iv.setPadding(8, 8, 8, 8);
			}
			else
			{
				v = convertView;
			}
			return v;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
	}
}
